package com.example.demo.test;/**
 * Created by kete on 2018-12-03.
 */

import com.alibaba.fastjson.JSONObject;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 作者：kete
 * 创建时间：2018-12-03 11:25
 * 功能描述：
 * 版本：
 */
public class MybatisUtil {

    public final static String SERVER_URL= "http://10.4.19.183:8093/i_service/bridge_zwyzs";

    public final static String SERVER_URL_PROD= "http://10.4.19.167:8080/i_service/bridge_zwyzs";

    private static SqlSessionFactory ssf;
    private static SqlSession ss;

    public synchronized static void init(){

        if(ssf!=null) return ;

        InputStream in = null;

        try {
            in = Resources.getResourceAsStream("mybatis.xml");

            ssf = new SqlSessionFactoryBuilder().build(in);
            in.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public synchronized static SqlSession getSqlSession(){
        if(ssf == null ) init();

        return ssf.openSession();

    }

    public static void main(String[] args) {
        ss = MybatisUtil.getSqlSession();

        TempUrlMapper mapper = ss.getMapper(TempUrlMapper.class);


        List<TempUrl> list1 = mapper.queryList("3");

        List<TempUrl> list = new ArrayList<>();

        for (int i=0;i<10;i++){
            TempUrl temp = list1.get(i);
            temp.setNewUrl("www.hao123.com"+i);
            list.add(temp);
//
//            mapper.updateUrl(temp.getAccountNo(),temp.getNewUrl());
        }

        mapper.updateBatch(list);



//        List<Map<String,String>> list = mapper.queryNum();
//
//        list.forEach(map->{
//            map.entrySet().forEach(entiry->{
//                System.out.println(entiry.getValue());
//            });
//        });

//        List<TempUrl> list = ss.selectList("com.example.demo.test.TempUrlMapper.queryNum");
//
//        String url = "http://10.4.19.183:8093/i_service/bridge_zwyzs";
//        list.forEach(model->{
//            System.out.println(model.toString());
//        });
//        list.forEach(model->{
//            try {
//                String msg = HttpClientUtil.me().httpPost(url,model.getAccountNo(),model.getDt());
//                JSONObject msgJson = JSONObject.parseObject(msg);
//                JSONObject respJson =  JSONObject.parseObject((CryptAesUtil.decrypt(msgJson.getString("msg"))));
//                System.out.println(msg);
//                System.out.println(respJson.getString("url"));
//                model.setNewUrl(respJson.getString("url"));
//            } catch (Exception e) {
//                e.printStackTrace();
//            }
//        });

//        list.forEach(model->{
//            System.out.println(model.toString());
//        });

        ss.close();
    }
}
